
package com.platform.event.core;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.alibaba.fastjson.JSONObject;
import com.platform.event.core.anno.EventHandler;
import com.platform.event.core.contants.EventConstants;
import com.platform.event.core.model.AbstractEventHandler;
import com.platform.event.core.model.BaseEvent;
import com.platform.event.core.model.EventResult;

public class EventHandleHelper
		implements   BeanPostProcessor, ApplicationContextAware {
	private static final long serialVersionUID = -7582802454287589552L;

	private static final Logger logger = LoggerFactory.getLogger(EventHandleHelper.class);

	private String annotationPackage;

	private String[] annotationPackages;
	
	private ApplicationContext applicationContext;
	
	private Map<String, AbstractEventHandler> eventHandlerMap = new HashMap<String, AbstractEventHandler>();

	
	public AbstractEventHandler getHandler(String name) {
		return eventHandlerMap.get(name);
	}

	public void handle(BaseEvent event, EventService eventService) {
		AbstractEventHandler eventHandle = getHandler(event.getEventName());
		logger.info("处理事件 event:" + JSONObject.toJSONString(event) + ",获取处理器handle:"
				+ JSONObject.toJSONString(eventHandle));
		// 如果处理器不为空，则处理事件
		// 获取本机信息
		EventResult result = new EventResult();
		try {
			eventService.setEventDescription(event);
			if (eventHandle != null) {
				result = eventHandle.handle(event);
			} else {
				result = new EventResult(true, EventConstants.NO_HANDLER_ERROR, null);
			}
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage(e.toString());
		} finally {
			logger.info("事件处理结果eventId:" + event.getId() + ",result:" + JSONObject.toJSONString(result));
			// 是否成功进行回调 修改事件状态
			eventService.handleCallBack(event, result);
			
		}

	}



	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		EventHandler handler = bean.getClass().getAnnotation(EventHandler.class);
		if (handler != null) {
			AbstractEventHandler eventHandler = (AbstractEventHandler) bean;
			eventHandler.setName(handler.name());
			eventHandlerMap.put(handler.name(), eventHandler);
		}
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		EventHandler handler = bean.getClass().getAnnotation(EventHandler.class);
		if (handler != null) {
			AbstractEventHandler eventHandler = (AbstractEventHandler) bean;
			eventHandler.setName(handler.name());
			eventHandlerMap.put(handler.name(), eventHandler);
		}
		return bean;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext=applicationContext;
	}




}